package com.hsd.znsh.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hsd.znsh.entity.Monitor;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.properties.YsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-06-25 15:07:02
 */
@Service
public class YsService {

    @Autowired
    private YsProperties ysProperties;

    @Autowired
    private RestTemplate restTemplate;

    private String accessToken;

    private LocalDateTime time;


    /*登录获取jwt*/
    private void login(){
        if (this.accessToken == null || (this.accessToken != null && isExpired())) {
            String url = "https://open.ys7.com/api/lapp/token/get?appKey={appKey}&appSecret={appSecret}";
            String response=restTemplate.postForObject(url,null,String.class,
                    ysProperties.getAppKey(),ysProperties.getAppSecret());
            this.accessToken=JSONObject.parseObject(response).getJSONObject("data").getString("accessToken");
            this.time=LocalDateTime.now();
        }
    }

    /*token是否失效*/
    private boolean isExpired(){
        if(this.time.plusDays(6).isBefore(LocalDateTime.now())) return true;
        return false;
    }

    private void sleep(long millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public boolean add(Monitor monitor) {
        login();
        String addUrl = "https://open.ys7.com/api/lapp/device/add?accessToken={accessToken}&deviceSerial={deviceSerial}&validateCode={validateCode}";
        String response=restTemplate.postForObject(addUrl,null,String.class,
                this.accessToken,monitor.getMonitorSn(),monitor.getCode());
        if(JSONObject.parseObject(response).getInteger("code")!=200){
            throw new ZnshException(JSONObject.parseObject(response).getString("msg"));
        }
        if(isEncrypt(monitor.getMonitorSn())){
            if(!offUrl(monitor)){
                while(!delete(monitor.getMonitorSn())){
                    sleep(1000);
                }
                return false;
            }
        }
        if(!openUrl(monitor)){
            while(!delete(monitor.getMonitorSn())){
                sleep(1000);
            }
            return false;
        }
        if(!getUrl(monitor)){
            while(!delete(monitor.getMonitorSn())){
                sleep(1000);
            }
            return false;
        }
        return true;
    }

    /*查询视频流地址*/
    public boolean getUrl(Monitor monitor){
        String getUrl="https://open.ys7.com/api/lapp/live/address/get?accessToken={accessToken}&source={source}";
        String response=restTemplate.postForObject(getUrl,null,String.class,
                this.accessToken,monitor.getMonitorSn()+":1");
        if(JSONObject.parseObject(response).getInteger("code")!=200){
            System.out.println(JSONObject.parseObject(response).getString("msg"));
            return false;
        }else{
            JSONArray  jsonArray=JSONObject.parseObject(response).getJSONArray("data");
            monitor.setHls(jsonArray.getJSONObject(0).getString("hls"));
            monitor.setHlsHd(jsonArray.getJSONObject(0).getString("hlsHd"));
            return true;
        }
    }

    /*打开视频直播*/
    public boolean openUrl(Monitor monitor){
        String openUrl="https://open.ys7.com/api/lapp/live/video/open?accessToken={accessToken}&source={source}";
        String response=restTemplate.postForObject(openUrl,null,String.class,
                this.accessToken,monitor.getMonitorSn()+":1");
        if(JSONObject.parseObject(response).getInteger("code")!=200){
            System.out.println(JSONObject.parseObject(response).getString("msg"));
            return false;
        }
        return true;
    }

    /*关闭视频加密*/
    public boolean offUrl(Monitor monitor){
        String offUrl = "https://open.ys7.com/api/lapp/device/encrypt/off?accessToken={accessToken}&deviceSerial={deviceSerial}&validateCode={validateCode}";
        String response=restTemplate.postForObject(offUrl,null,String.class,
                this.accessToken,monitor.getMonitorSn(),monitor.getCode());
        if(JSONObject.parseObject(response).getInteger("code")!=200){
            System.out.println(JSONObject.parseObject(response).getString("msg"));
            return false;
        }
        return true;
    }

    /*是否视频加密*/
    public boolean isEncrypt(String deviceSerial){
        login();
        String openUrl="https://open.ys7.com/api/lapp/device/info?accessToken={accessToken}&deviceSerial={deviceSerial}";
        String response=restTemplate.postForObject(openUrl,null,String.class,
                this.accessToken,deviceSerial);
        if(JSONObject.parseObject(response).getInteger("code")!=200){
            throw new ZnshException(JSONObject.parseObject(response).getString("msg"));
        }else{
            if(JSONObject.parseObject(response).getJSONObject("data").getInteger("isEncrypt")==0) return false;
            return true;
        }
    }

    /*删除*/
    public boolean delete(String deviceSerial){
        login();
        String url = "https://open.ys7.com/api/lapp/device/delete?accessToken={accessToken}&deviceSerial={deviceSerial}";
        String response=restTemplate.postForObject(url,null,String.class,
                this.accessToken,deviceSerial);
        if(JSONObject.parseObject(response).getInteger("code")!=200){
            System.out.println(JSONObject.parseObject(response).getString("msg"));
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        YsService ysService=new YsService();
        ysService.delete("141505844");
        //System.out.println(ysService.add("141505844","PJABWI"));
    }



}

